//
//  JUFNUserAccount.swift
//  WiBoPM
//
//  Created by 陈俊峰 on 2017/1/8.
//  Copyright © 2017年 陈俊峰. All rights reserved.
//

import UIKit

class JUFNUserAccount: NSObject,NSCoding{
    public func encode(with aCoder: NSCoder) {
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(expires_in, forKey: "expires_in")
        aCoder.encode(expires_date, forKey: "expires_date")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(screen_name, forKey: "screen_name")
        aCoder.encode(avatar_large, forKey: "avatar_large")
    }
    required init?(coder aDecoder: NSCoder) {
        self.avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
        self.screen_name = aDecoder.decodeObject(forKey: "screen_name") as? String
        self.uid = aDecoder.decodeObject(forKey: "uid") as? String
        self.expires_date = aDecoder.decodeObject(forKey: "expires_date") as? NSDate as Date?
        self.expires_in = aDecoder.decodeDouble(forKey: "expires_in")
        self.access_token = aDecoder.decodeObject(forKey: "access_token") as? String
    }

    ///用户授权的唯一票据，用于调用微博的开放接口，同时也是第三方应用验证微博用户登录的唯一票据，第三方应用应该用该票据和自己应用内的用户建立唯一影射关系，来识别登录状态，不能使用本返回值里的UID字段来做登录识别。
    var access_token:String?
    ///access_token的生命周期，单位是秒数。
    var expires_in:Double = 0 {
        //将间隔时间,转化为时间
        didSet{
            expires_date = NSDate.init(timeIntervalSinceNow: expires_in) as Date
        }
    }
    ///授权用户的UID，本字段只是为了方便开发者，减少一次user/show接口调用而返回的，第三方应用不能用此字段作为用户登录状态的识别，只有access_token才是用户授权的唯一票据。
    var uid:String?
    
    var screen_name:String?
    
    var avatar_large:String?
    
    var expires_date:Date?
    
    
    //提供一个kvc创建对象的方法.
    init(dict:[String:Any?]){
        super.init()
        setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
    }
    
//    override var description: String{
//        let properties = ["access_token","expires_in","expires_date","uid,screen_name","avatar_large"]
//        return "\(dictionaryWithValues(forKeys: properties))"
//    }
    
    
}
